const sqlOptions = require("../../sql");
const stat = require("express").Router()
// 首页
stat.get("/stat", async (req, res) => {
    try {
        // 物流状态
        const logistics = await req.db.query(sqlOptions({
            name: "oms_order_logistics",
            method: "list",
            data: {},
            select: "logistics_status"
        }))
        const obj = {
            "待揽收": 0,
            "揽收中": 0,
            "运输中": 0,
            "中转中": 0,
            "清关中": 0,
            "发货到达": 0,
            "派送中": 0,
            "派送失败": 0,
            "派送异常": 0,
            "派送到达": 0,
            "签收": 0,
            "已下单": 0,
            "已发货": 0,
            "已出库": 0,
        }
        logistics.forEach(item => {
            if (item.logistics_status === '0') {
                obj["已下单"] = obj["已下单"] + 1
            }
            if (item.logistics_status === '1') {
                obj["已发货"] = obj["已发货"] + 1
            }
            if (item.logistics_status === '2') {
                obj["已出库"] = obj["已出库"] + 1
            }
            if (item.logistics_status === '3') {
                obj["待揽收"] = obj["待揽收"] + 1
            }
            if (item.logistics_status === '4') {
                obj["揽收中"] = obj["揽收中"] + 1
            }
            if (item.logistics_status === '5') {
                obj["运输中"] = obj["运输中"] + 1
            }
            if (item.logistics_status === '6') {
                obj["中转中"] = obj["中转中"] + 1
            }
            if (item.logistics_status === '7') {
                obj["清关中"] = obj["清关中"] + 1
            }
            if (item.logistics_status === '8') {
                obj["发货到达"] = obj["发货到达"] + 1
            }
            if (item.logistics_status === '9') {
                obj["派送中"] = obj["派送中"] + 1
            }
            if (item.logistics_status === '10') {
                obj["派送失败"] = obj["派送失败"] + 1
            }
            if (item.logistics_status === '11') {
                obj["派送异常"] = obj["派送异常"] + 1
            }
            if (item.logistics_status === '12') {
                obj["派送到达"] = obj["派送到达"] + 1
            }
            if (item.logistics_status === '13') {
                obj["签收"] = obj["签收"] + 1
            }
        })

        // 订单状态
        const order = await req.db.query(sqlOptions({
            name: "oms_order",
            method: "list",
            data: {},
            select: "order_status"
        }))
        const orderObj = {
            "未付款": 0,
            "已付款": 0,
            "已签收": 0,
            "确认收货": 0,
            "申请退货": 0,
            "退货中": 0,
            "已退货": 0,
            // "确认退货": 0,
        }
        // console.log(order)
        order.forEach(item => {
            if (item.order_status === '0') {
                orderObj["未付款"] = orderObj["未付款"] + 1
            }
            if (item.order_status === '1') {
                orderObj["已付款"] = orderObj["已付款"] + 1
            }
            if (item.order_status === '2') {
                orderObj["已发货"] = orderObj["已发货"] + 1
            }
            if (item.order_status === '3') {
                orderObj["已签收"] = orderObj["已签收"] + 1
            }
            if (item.order_status === '4') {
                orderObj["确认收货"] = orderObj["确认收货"] + 1
            }
            if (item.order_status === '5') {
                orderObj["退货中"] = orderObj["退货中"] + 1
            }
            if (item.order_status === '6') {
                orderObj["已退货"] = orderObj["已退货"] + 1
            }
            // if (item.order_status === '7') {
            //     orderObj["确认退货"] = orderObj["确认退货"] + 1
            // }
            if (item.order_status === '7') {
                orderObj["申请退货"] = orderObj["申请退货"] + 1
            }
        })

        return res.status(200).send({
            code: 200,
            message: "",
            data: {
                "订单状态": orderObj,
                "物流状态": obj
            }
        })
    } catch (e) {
        return res.status(500).send({
            code: 500,
            message: "系统错误",
            data: ""
        })
    }
})

module.exports = stat
